import flask

from main.link.sshLink import sshLink
from main.thread.apiGoThread import apiGoThread
from main.tools.iuGetpp import iuGetpp
from main.tools.setHttpOrganization import after_request

api = flask.Flask(__name__)
api.after_request(after_request)

@api.route('/main', methods=['post'])
def main():
    cmd = flask.request.values.get('cmd')

    ip = flask.request.values.get('ip')
    user = flask.request.values.get('user')
    name = flask.request.values.get('name')
    passwd,port = iuGetpp(ip,user,name)
    size = flask.request.values.get('size')
    timeout = flask.request.values.get('timeout')

    sshChannel = sshLink(ip, port.__str__(), user, passwd, size, timeout)

    sshChannel.set_name(ip)
    sshChannel.send(cmd)
    out = sshChannel.recv(65535)

    return out

if __name__ == '__main__':
    api.run(port=8080, debug=False, host='127.0.0.1')
